package com.jin.gulimall.product.service.impl;

import com.jin.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.jin.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.jin.gulimall.product.entity.AttrEntity;
import com.jin.gulimall.product.service.AttrService;
import com.jin.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.jin.gulimall.product.vo.AttrRelationVO;
import com.jin.gulimall.product.vo.SkuItemVo;
import com.jin.gulimall.product.vo.SpuItemGroupAttrVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jin.common.utils.PageUtils;
import com.jin.common.utils.Query;

import com.jin.gulimall.product.dao.AttrGroupDao;
import com.jin.gulimall.product.entity.AttrGroupEntity;
import com.jin.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    //自定义的分页信息，新增了catelogId属性
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //实现模糊查询
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=catelogId
        // and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        //Long类型和基本数据类型比较时会自动拆箱
        if(catelogId==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    @Override
    public void deleteAttrRelation(AttrRelationVO[] vos) {
        //relationDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>())
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, entity);
            return entity;
        }).collect(Collectors.toList());
        //构造一个方法，可以批量删除AttrAttrgroupRelationEntity
        relationDao.deleteBatchRelation(entities);
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsBycatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVO> collect = groupEntities.stream().map((group) -> {
            AttrGroupWithAttrsVO vo = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(group,vo);
            Long attrGroupId = vo.getAttrGroupId();
            List<AttrEntity> attrEntities = attrService.getAttrGroup(attrGroupId);
            vo.setAttrs(attrEntities);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemGroupAttrVO> getSpuAttrsWithSpu(Long catalogId, Long spuId) {
        AttrGroupDao dao = this.getBaseMapper();
        List<SpuItemGroupAttrVO> vos=dao.getSpuAttrsWithSpu(catalogId,spuId);
        return vos;
    }

}